
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 04/25/2013 21:07:23
-- Generated from EDMX file: C:\GroCms\Entities\Entities.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Cms];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_DocumentPage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Document] DROP CONSTRAINT [FK_DocumentPage];
GO
IF OBJECT_ID(N'[dbo].[FK_PageLeftSideContent]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Page] DROP CONSTRAINT [FK_PageLeftSideContent];
GO
IF OBJECT_ID(N'[dbo].[FK_NewsPage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[News] DROP CONSTRAINT [FK_NewsPage];
GO
IF OBJECT_ID(N'[dbo].[FK_PageRightSideContent]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Page] DROP CONSTRAINT [FK_PageRightSideContent];
GO
IF OBJECT_ID(N'[dbo].[FK_PhotoAlbumPage]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PhotoAlbum] DROP CONSTRAINT [FK_PhotoAlbumPage];
GO
IF OBJECT_ID(N'[dbo].[FK__PhotoAlbu__MainP__34C8D9D1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PhotoAlbum] DROP CONSTRAINT [FK__PhotoAlbu__MainP__34C8D9D1];
GO
IF OBJECT_ID(N'[dbo].[FK_PhotoPhotoAlbum]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Photo] DROP CONSTRAINT [FK_PhotoPhotoAlbum];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Document]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Document];
GO
IF OBJECT_ID(N'[dbo].[LeftSideRenderer]', 'U') IS NOT NULL
    DROP TABLE [dbo].[LeftSideRenderer];
GO
IF OBJECT_ID(N'[dbo].[News]', 'U') IS NOT NULL
    DROP TABLE [dbo].[News];
GO
IF OBJECT_ID(N'[dbo].[Page]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Page];
GO
IF OBJECT_ID(N'[dbo].[Photo]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Photo];
GO
IF OBJECT_ID(N'[dbo].[RightSideRenderer]', 'U') IS NOT NULL
    DROP TABLE [dbo].[RightSideRenderer];
GO
IF OBJECT_ID(N'[dbo].[User]', 'U') IS NOT NULL
    DROP TABLE [dbo].[User];
GO
IF OBJECT_ID(N'[dbo].[PhotoAlbum]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PhotoAlbum];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Document'
CREATE TABLE [dbo].[Document] (
    [Id] bigint IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(128)  NOT NULL,
    [Description] nvarchar(512)  NULL,
    [PageId] bigint  NOT NULL
);
GO

-- Creating table 'LeftSideRenderer'
CREATE TABLE [dbo].[LeftSideRenderer] (
    [Id] bigint IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(128)  NOT NULL,
    [Description] nvarchar(512)  NULL,
    [Code] nvarchar(128)  NOT NULL
);
GO

-- Creating table 'News'
CREATE TABLE [dbo].[News] (
    [Id] bigint IDENTITY(1,1) NOT NULL,
    [Title] nvarchar(128)  NOT NULL,
    [Body] nvarchar(max)  NOT NULL,
    [Order] bigint  NOT NULL,
    [PageId] bigint  NOT NULL
);
GO

-- Creating table 'Page'
CREATE TABLE [dbo].[Page] (
    [Id] bigint IDENTITY(1,1) NOT NULL,
    [Title] nvarchar(128)  NOT NULL,
    [Description] nvarchar(512)  NULL,
    [LeftSideRendererId] bigint  NOT NULL,
    [RightSideRendererId] bigint  NOT NULL,
    [Order] bigint  NOT NULL
);
GO

-- Creating table 'Photo'
CREATE TABLE [dbo].[Photo] (
    [Id] bigint IDENTITY(1,1) NOT NULL,
    [Title] nvarchar(128)  NULL,
    [Description] nvarchar(512)  NULL,
    [Order] bigint  NOT NULL,
    [PhotoAlbumId] bigint  NOT NULL,
    [Version] bigint  NOT NULL
);
GO

-- Creating table 'RightSideRenderer'
CREATE TABLE [dbo].[RightSideRenderer] (
    [Id] bigint IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(128)  NOT NULL,
    [Description] nvarchar(512)  NULL,
    [Code] nvarchar(128)  NOT NULL
);
GO

-- Creating table 'User'
CREATE TABLE [dbo].[User] (
    [Id] bigint IDENTITY(1,1) NOT NULL,
    [Login] nvarchar(128)  NOT NULL,
    [Password] nvarchar(128)  NOT NULL
);
GO

-- Creating table 'PhotoAlbum'
CREATE TABLE [dbo].[PhotoAlbum] (
    [Id] bigint IDENTITY(1,1) NOT NULL,
    [Title] nvarchar(128)  NOT NULL,
    [Description] nvarchar(512)  NOT NULL,
    [Order] bigint  NOT NULL,
    [PageId] bigint  NOT NULL,
    [MainPhotoId] bigint  NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Document'
ALTER TABLE [dbo].[Document]
ADD CONSTRAINT [PK_Document]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'LeftSideRenderer'
ALTER TABLE [dbo].[LeftSideRenderer]
ADD CONSTRAINT [PK_LeftSideRenderer]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'News'
ALTER TABLE [dbo].[News]
ADD CONSTRAINT [PK_News]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Page'
ALTER TABLE [dbo].[Page]
ADD CONSTRAINT [PK_Page]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Photo'
ALTER TABLE [dbo].[Photo]
ADD CONSTRAINT [PK_Photo]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'RightSideRenderer'
ALTER TABLE [dbo].[RightSideRenderer]
ADD CONSTRAINT [PK_RightSideRenderer]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'User'
ALTER TABLE [dbo].[User]
ADD CONSTRAINT [PK_User]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'PhotoAlbum'
ALTER TABLE [dbo].[PhotoAlbum]
ADD CONSTRAINT [PK_PhotoAlbum]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [PageId] in table 'Document'
ALTER TABLE [dbo].[Document]
ADD CONSTRAINT [FK_DocumentPage]
    FOREIGN KEY ([PageId])
    REFERENCES [dbo].[Page]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DocumentPage'
CREATE INDEX [IX_FK_DocumentPage]
ON [dbo].[Document]
    ([PageId]);
GO

-- Creating foreign key on [LeftSideRendererId] in table 'Page'
ALTER TABLE [dbo].[Page]
ADD CONSTRAINT [FK_PageLeftSideContent]
    FOREIGN KEY ([LeftSideRendererId])
    REFERENCES [dbo].[LeftSideRenderer]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PageLeftSideContent'
CREATE INDEX [IX_FK_PageLeftSideContent]
ON [dbo].[Page]
    ([LeftSideRendererId]);
GO

-- Creating foreign key on [PageId] in table 'News'
ALTER TABLE [dbo].[News]
ADD CONSTRAINT [FK_NewsPage]
    FOREIGN KEY ([PageId])
    REFERENCES [dbo].[Page]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_NewsPage'
CREATE INDEX [IX_FK_NewsPage]
ON [dbo].[News]
    ([PageId]);
GO

-- Creating foreign key on [RightSideRendererId] in table 'Page'
ALTER TABLE [dbo].[Page]
ADD CONSTRAINT [FK_PageRightSideContent]
    FOREIGN KEY ([RightSideRendererId])
    REFERENCES [dbo].[RightSideRenderer]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PageRightSideContent'
CREATE INDEX [IX_FK_PageRightSideContent]
ON [dbo].[Page]
    ([RightSideRendererId]);
GO

-- Creating foreign key on [PageId] in table 'PhotoAlbum'
ALTER TABLE [dbo].[PhotoAlbum]
ADD CONSTRAINT [FK_PhotoAlbumPage]
    FOREIGN KEY ([PageId])
    REFERENCES [dbo].[Page]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PhotoAlbumPage'
CREATE INDEX [IX_FK_PhotoAlbumPage]
ON [dbo].[PhotoAlbum]
    ([PageId]);
GO

-- Creating foreign key on [MainPhotoId] in table 'PhotoAlbum'
ALTER TABLE [dbo].[PhotoAlbum]
ADD CONSTRAINT [FK__PhotoAlbu__MainP__34C8D9D1]
    FOREIGN KEY ([MainPhotoId])
    REFERENCES [dbo].[Photo]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK__PhotoAlbu__MainP__34C8D9D1'
CREATE INDEX [IX_FK__PhotoAlbu__MainP__34C8D9D1]
ON [dbo].[PhotoAlbum]
    ([MainPhotoId]);
GO

-- Creating foreign key on [PhotoAlbumId] in table 'Photo'
ALTER TABLE [dbo].[Photo]
ADD CONSTRAINT [FK_PhotoPhotoAlbum]
    FOREIGN KEY ([PhotoAlbumId])
    REFERENCES [dbo].[PhotoAlbum]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PhotoPhotoAlbum'
CREATE INDEX [IX_FK_PhotoPhotoAlbum]
ON [dbo].[Photo]
    ([PhotoAlbumId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------